#ifndef ROM_H_
#define ROM_H_

#include "Misc.h"

class Rom {
  private:
    uint size;
    uchar* romBuffer;
    uint seekAddr;
    uint lastFreePos;
    uint firstFreePos;

    uchar bitrevs[256];
    void initBitrevs();
    void encode(uchar **bpos, int length, int type);
    void rencode(uchar **bpos, uchar *pos, int length);
  public:
    Rom();
    ~Rom();
    void open(char* fname);
    void openExpand(char* fname);
    void save(char* fname);
    void close();
    void seek(uint addr);
    uint tell();
    uchar read();
    uchar read(uint addr);
    void read(uchar* buf, int len);
    void read(uint addr, uchar* buf, int len);
    uint readMulti(uint len);
    uint readMulti(uint addr, uint len);
    void write(uchar byte);
    void write(uchar byte, uint addr);
    void write(uchar *data, uint addr, uint len);
    void writeMulti(uint data, uint len);
    void writeMulti(uint data, uint addr, uint len);

    uchar* getBuffer();

    uint getFreeRange(int len);
    uint getLastFreePos();
    void setFirstFreePos(uint ffP);

    int comp(uchar *udata, uchar *buffer, int length);
    int decomp(uint addr, uchar *buffer, int maxlen);
};

#endif
